

var dataUtils = {}

/**
 * 遍历集合
 * @param obj
 * @param fn
 * @returns {utils}
 */
dataUtils.each = function (obj, fn) {
    let key = this
    let that = this
    if (typeof fn !== 'function') return that
    obj = obj || []
    if (obj.constructor === Object) {
        for (key in obj) {
            if (fn.call(obj[key], key, obj[key])) break
        }
    } else {
        for (key = 0; key < obj.length; key++) {
            if (fn.call(obj[key], key, obj[key])) break
        }
    }
    return that
}

/**
 * 存储数据
 * @param key
 * @param value
 */
dataUtils.setData = function (key, value) {
    sessionStorage.setItem(key, JSON.stringify(value))
    // sessionStorage.setItem(key, JSON.stringify(value))
}

/**
 * 获取数据
 * @param key
 */
dataUtils.getData = function (key) {
    if (sessionStorage.getItem(key) == null) {
        return null
    }
    return sessionStorage.getItem(key)
    // return JSON.parse(sessionStorage.getItem(key))
}

/**
 * 移除数据
 * @param key
 */
dataUtils.removeData = function (key) {
    sessionStorage.removeItem(key)
}

/**
 * 移除数据
 * @param key
 */
dataUtils.removeAllData = function () {
    sessionStorage.clear()
}
/**
 * 格式化菜单数据
 * @param data
 * @param id
 * @returns {Array}
 */
dataUtils.getALLMenuTree = (data, id) => {
    var tree = []
    var i = 0
    data.forEach(function (item, index) {
        if (item.pid == id) {
            item.children = dataUtils.getALLMenuTree(data, item.id)
            if (item.children.length <= 0) {
                delete item.children
            }
            tree[i] = item
            i++
        }
    })
    return tree
}


/**
 * 删除彩蛋和子菜单
 * @param data
 * @param id
 * @returns {Array}
 * @name 李昂
 */
dataUtils.delALLMenuTree = (row,listId) => {
    listId.push(row.id)
    if(row.children!=null && row.children.length>0){
        for(var item in row.children){
            dataUtils.delALLMenuTree(row.children[item],listId)
        }
    }
    return listId
}

/**
 * 获取图片
 * @param url
 */
dataUtils.getImg = function (url) {
    return url //Setting.apiBaseURL + 'admin/sys-file/'+url
}


/**
 * 脱敏工具类
 * @param start 开始节点
 * @param end   结束节点
 * @param str1  脱敏字符串
 * @param str2  脱敏格式
 */
dataUtils.desensitization=function (start,end,str1){
  if (!str1){
    return "--"
  }else {
    let len = str1.length();
    let leftStr = str1.substring(0,start);
    let rightStr = str1.substring(end,len);
    let str = ''
    let i = 0;
    try {
      for (i = 0; i < end-start;i++) {
        str = str + '*';
      }
    } catch (error) {

    }
    str = leftStr + str + rightStr;
    return str;
  }
}
export default dataUtils
